----------------------------------
-- 好友逻辑
----------------------------------
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local FriendDBLogic = require("friend.FriendDBLogic")
local FriendDefine = require("friend.FriendDefine")
local RoleDBLogic = require("role.RoleDBLogic")
local CombatLogic = require("combat.CombatLogic")
local CombatDefine = require("combat.CombatDefine")
local CombatPosLogic = require("combat.CombatPosLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local Util = require("common.Util")
local MailExcel = require("excel.mail")
local MailManager = require("mail.MailManager")
local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
local RoleDefine = require("role.RoleDefine")
local RoleLogic = require("role.RoleLogic")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
local WarReportLogic = require("warReport.WarReportLogic")
local JjcDB = require("jjc.JjcDB")
local ChatRecord = require("chat.ChatRecord")
local CombatVideo = require("combat.CombatVideo")
local Log = require("common.Log")

local fields = { name = 1, lv = 1, lastLogoutTime = 1, head = 1, headFrame = 1, chenghao = 1, zhandouli = 1}
local tempHuman = { }
function makeFriendNet(net, uuid, human, getStatus, giveStatus)
    if net == nil then return end
    local targetDB, online = RoleDBLogic.getDb(uuid, fields)
    if not targetDB then return end
    RoleLogic.makeRoleBase(targetDB,net.roleBase,CombatDefine.COMBAT_TYPE1)
    if online then
        net.logoutSecond = 0
        net.online = 1
    else
        local lastTs = targetDB.lastLogoutTime or 0
        net.logoutSecond = os.time() - lastTs
        net.online = 0
    end
    net.getStatus = getStatus or 0
    net.giveStatus = giveStatus or 0
    return true
end

function sendFriendList(human)
    ObjHuman.updateDaily(human)
    local cnt, list = FriendDBLogic.getFriendUuids(human.db._id)
    local msgRet = Msg.gc.GC_FRIEND_LIST
    local questNum = FriendDBLogic.haveRequest(human.db._id)
    msgRet.questNum = questNum or 0
    msgRet.heartCnt = human.db.getHeartCnt and human.db.getHeartCnt > 0 and 1 or 0

    msgRet.friends[0] = 0
    local sendLen = 0
    for i = 1, cnt do
        sendLen = sendLen + 1
        local net = msgRet.friends[sendLen]
        local giveStatus = nil
        if human.db.sendHeart ~= nil then
            giveStatus = human.db.sendHeart[list[i].uuid]
        end
        makeFriendNet(net, list[i].uuid, human, list[i].getStatus, giveStatus)
        if sendLen >= FriendDefine.RECOMMEND_CNT then
            msgRet.isEnd = 0
            msgRet.friends[0] = sendLen
            Msg.send(msgRet, human.fd)
            sendLen = 0
        end
    end
    msgRet.isEnd = 1
    msgRet.friends[0] = sendLen
    Msg.send(msgRet, human.fd)
end

local recommendList = {}
function sendRecommend(human, uuid)
	for i = 1, #recommendList do
		recommendList[i] = nil
	end
	
	if uuid then
		recommendList[1] = uuid
	else
        local humanCnt = ObjHuman.getOnlineCnt()
        human.recommendTotal = math.ceil(humanCnt / 10)
        human.recommendIndex = human.recommendIndex or 0
        if human.recommendIndex >= human.recommendTotal then
           human.recommendIndex = 1
        else
           human.recommendIndex = human.recommendIndex + 1
        end
        local index = 0
        local cnt = 0
        for uuid, target in pairs(ObjHuman.onlineUuid) do
			if cnt >= 10 then
				break
			end
            index = index + 1
            if index >= (human.recommendIndex - 1) * 10 then
			   if uuid ~= human.db._id and
			   	not FriendDBLogic.isFriend(human.db._id, uuid) and
                not FriendDBLogic.hasRequest(human.db._id, uuid) and
                isBlackFriendCanDo(human.db._id,uuid) and 
                target.fd then
			   	  cnt = cnt + 1
			   	  recommendList[cnt] = uuid
			   end
            end
		end
	end

	local msgRet = Msg.gc.GC_FRIEND_RECOMMEND_LIST
	msgRet.friends[0] = 0
	local recommendCnt = #recommendList
	for i = 1, recommendCnt do
		if i > FriendDefine.RECOMMEND_CNT then
			break
		end
		local r = math.random(i, recommendCnt)
		recommendList[i], recommendList[r] = recommendList[r], recommendList[i]
		local net = msgRet.friends[msgRet.friends[0] + 1]
		if makeFriendNet(net, recommendList[i], human) then
			msgRet.friends[0] = msgRet.friends[0] + 1
		end
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

function sendRequestList(human, isLogin)
	local list = FriendDBLogic.getRequests(human.db._id)
	local msgRet = Msg.gc.GC_FRIEND_REQUEST_LIST
	msgRet.friends[0] = 0
	if list then
		for uuid in pairs(list) do
			local net = msgRet.friends[msgRet.friends[0] + 1]
			if makeFriendNet(net, uuid, human) then
				msgRet.friends[0] = msgRet.friends[0] + 1
			end
		end
	end
	if isLogin and msgRet.friends[0] < 1 then
		return
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

local function getLen(tb)
	if not tb then return 0 end

	local len = 0
	for _ in pairs(tb) do
		len = len + 1
	end
	return len
end

function addFriend(human, uuid)
    -- 自己不能加自己为好友
    local targetDB = nil
    if human.db._id == uuid then 
    	Broadcast.sendErr(human,Lang.FRIEND_ADD_BENREN)
    	return 
    end
    targetDB = RoleDBLogic.getDb(uuid, fields)
    -- 判断这个人是否存在
    if not targetDB then 
       Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_NOTHAD)
       return 
    end

    --是否拉黑
    if not isBlackFriendCanDo(human.db._id,uuid,true) then return end

	-- 是否已经是好友了
	if FriendDBLogic.isFriend(human.db._id, uuid) then
		Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_HAD)
		return
	end

	-- 是否已经申请好友
	if FriendDBLogic.hasRequest(human.db._id, uuid) then
		Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_REQUEST)
		return
	end

	-- 对方的申请列表是否已满
	local list = FriendDBLogic.getRequests(uuid)
	local cnt = getLen(list)
	if cnt >= FriendDefine.REQUEST_CNT_MAX then
		Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_REQUEST_MAX)
		return
	end

	local cnt2 = FriendDBLogic.getFriendUuids(human.db._id)
	if cnt2 >= FriendDefine.FRIEND_MAX_CNT then
		Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_CNT)		
		return
	end	

	FriendDBLogic.addRequest(human.db._id, uuid)
	-- 通知对方有人申请
	
	local targetHuman = ObjHuman.onlineUuid[uuid]
	if targetHuman and targetHuman.fd then
		local msgRet = Msg.gc.GC_FRIEND_ADD_NOTIFY
		makeFriendNet(msgRet.friend, human.db._id, human)
		Msg.send(msgRet, targetHuman.fd)
		
		RoleSystemLogic.onDot(targetHuman, RoleSystemDefine.ROLE_SYS_ID_205)
	end

	-- 申请成功提示
	Broadcast.sendErr(human, Lang.FRIEND_ADD_OK)

	local msgRet = Msg.gc.GC_FRIEND_ADD_OK
	msgRet.uuid = uuid
	Msg.send(msgRet, human.fd)
end

function agreeFriend(human, uuid)
	--是否拉黑
    if not isBlackFriendCanDo(human.db._id,uuid,true) then return end

	-- 是否有申请
	if not FriendDBLogic.hasRequest(uuid, human.db._id) then
		return
	end

	-- 好友已满了，不能继续添加
	local cnt = FriendDBLogic.getFriendUuids(human.db._id)
	if cnt >= FriendDefine.FRIEND_MAX_CNT then
		Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_CNT)		
		return
	end	

	-- 对方的好友已满
	local cnt2 = FriendDBLogic.getFriendUuids(uuid)
	if cnt2 >= FriendDefine.FRIEND_MAX_CNT then
		Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_CNT2)		
		return
	end	

	-- 删除申请
	delRequest(human.db._id, uuid)
	delRequest(uuid, human.db._id)

	if FriendDBLogic.addFriendData(human.db._id, uuid) then
		-- 添加成功
		local msgRet = Msg.gc.GC_FRIEND_ADD
		makeFriendNet(msgRet.friend, uuid, human)
		Msg.send(msgRet, human.fd)	
		Broadcast.sendErr(human, Lang.FRIEND_ADD_OK2)
		
		local target = ObjHuman.onlineUuid[uuid]
		if target and target.fd then
			msgRet = Msg.gc.GC_FRIEND_ADD
			makeFriendNet(msgRet.friend, human.db._id, target)
            Broadcast.sendErr(target, Util.format(Lang.FRIEND_ADD_OK3,human.db.name) )
			Msg.send(msgRet, target.fd)
		end	
		
		RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
		RoleSystemLogic.onDotByUuid(uuid, RoleSystemDefine.ROLE_SYS_ID_205)

        
        ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_13,cnt + 1)
        ChengjiuLogic.onCallbackByUuid(uuid,ChengjiuDefine.CJ_TASK_TYPE_13,cnt2 + 1)
	else
		-- 已经是好友的时候要提示
		Broadcast.sendErr(human, Lang.FRIEND_ADD_ERR_HAD)
	end
end

function refuseFriend(human, uuid)
	-- 是否有申请
	if not FriendDBLogic.hasRequest(uuid, human.db._id) then
		return
	end

	-- 删除申请
	delRequest(uuid, human.db._id)
	
	sendRequestList(human)
	
	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
end

-- 删除uuid1向uuid2的好友申请
function delRequest(uuid1, uuid2, nosend)
	local list = FriendDBLogic.getRequests(uuid2)
	if list and list[uuid1] then
		list[uuid1] = nil
		local target = ObjHuman.onlineUuid[uuid2]
		if target and target.fd and not nosend then
			local msgRet = Msg.gc.GC_FRIEND_REQUEST_DEL
			msgRet.uuid = uuid1
			Msg.send(msgRet, target.fd)
		end
	end		
end

function refuseFriendAll(human)
    local msgRet = Msg.gc.GC_FRIEND_REQUEST_DEL
	local list = FriendDBLogic.getRequests(human.db._id)
	if list then
		for uuid in pairs(list) do
		    msgRet.uuid = uuid
		    Msg.send(msgRet, human.fd)
            list[uuid] = nil
		end
	end
	
	sendRequestList(human)
	
	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
end

local function agreeAllDo(uuid,human)
	--是否拉黑
    if not isBlackFriendCanDo(human.db._id,uuid) then return end

	-- 好友已满了，不能继续添加
	local cnt = FriendDBLogic.getFriendUuids(human.db._id)
	if cnt >= FriendDefine.FRIEND_MAX_CNT then
		return
	end	

	-- 对方的好友已满
	local cnt2 = FriendDBLogic.getFriendUuids(uuid)
	if cnt2 >= FriendDefine.FRIEND_MAX_CNT then		
		return
	end	

	-- 删除申请
	delRequest(human.db._id, uuid, true)
	delRequest(uuid, human.db._id)

	if FriendDBLogic.addFriendData(human.db._id, uuid) then
		local target = ObjHuman.onlineUuid[uuid]
		if target and target.fd then
			msgRet = Msg.gc.GC_FRIEND_ADD
			makeFriendNet(msgRet.friend, human.db._id, target)
			Msg.send(msgRet, target.fd)
		end	
        

		RoleSystemLogic.onDotByUuid(uuid, RoleSystemDefine.ROLE_SYS_ID_205)

        ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_13,cnt + 1)
        ChengjiuLogic.onCallbackByUuid(uuid,ChengjiuDefine.CJ_TASK_TYPE_13,cnt2 + 1)

        return true
	else
	end
end

function CG_FRIEND_AGREE_ALL(human)
	local list = FriendDBLogic.getRequests(human.db._id)
	local agreeOK = nil
	if list then
		for uuid in pairs(list) do
			if agreeAllDo(uuid,human) then
               agreeOK = true
            end
		end
	end

	if agreeOK then
		RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
	end

	sendRequestList(human)
end


function delFriend(human, uuid)
	-- 是否已经是好友了
	if not FriendDBLogic.isFriend(human.db._id, uuid) then
		return
	end	

	FriendDBLogic.delFriendData(human.db._id, uuid)

	local msgRet = Msg.gc.GC_FRIEND_DEL_NOTIFY
	msgRet.uuid = uuid
	Msg.send(msgRet, human.fd)

	local target = ObjHuman.onlineUuid[uuid]
	if target and target.fd then
		msgRet.uuid = human.db._id
		Msg.send(msgRet, target.fd)
	end

	Broadcast.sendErr(human, Lang.FRIEND_DEL_OK)

	ChatRecord.delFriendChatRecordDouble(human.db._id,uuid)
	
	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
	RoleSystemLogic.onDotByUuid(uuid, RoleSystemDefine.ROLE_SYS_ID_205)
end

-- 检查可否挑战
function checkCombatPos(human, args)
	local uuid = args[1]	
	-- 判断是不是好友，不是好友也能切磋
	--if not uuid or not FriendDBLogic.isFriend(human.db._id, uuid) then
	--	return Broadcast.sendErr(human, Lang.FRIEND_COMBAT_ERR_NOT_FRIEND)
	--end

	local fakeHuman = CombatLogic.createCombatFakeHuman(uuid)
	if not fakeHuman then return end

	local combatHero = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE1, nil, true)
	if not combatHero or not next(combatHero) then
		return Broadcast.sendErr(human, Lang.FRIEND_COMBAT_ERR_NO_DEFENCE)
	end
	return true
end

-- 获取战斗目标
function getCombatObjList(human, side, args)	
	if side ~= CombatDefine.DEFEND_SIDE then
		return
	end
	local uuid = args[1]
	if not uuid then return end
	local fakeHuman = CombatLogic.createCombatFakeHuman(uuid)
	if not fakeHuman then return end
	return CombatLogic.getHumanObjList(fakeHuman, CombatDefine.COMBAT_TYPE1)
end

-- 好友切磋
function fight(human, args)
	if not checkCombatPos(human, args) then
		return
	end
	
	-- 开打
    local targetUuid = args[1]
	CombatLogic.combatBegin(human, 1001, args, CombatDefine.COMBAT_TYPE14, targetUuid)
end


function onFightEnd(human, result, combatType, cbParam, combatInfo, isSaodang)
    local videoUuid = CombatVideo.saveCombatVideo(human, true)
    if videoUuid then		
       local msgRet = Msg.gc.GC_COMBAT_RETURN_RECORD
       msgRet.videoUuid = videoUuid
       msgRet.type = combatType
       Msg.send(msgRet,human.fd)
    end

    local jjcRank = JjcDB.getRank(human.db._id)
    if jjcRank >= WarReportLogic.WAR_FRIEND_RANK then return end

    -- 添加战报
    local targetUuid = cbParam
    local jjcTargetRank = JjcDB.getRank(targetUuid)
    WarReportLogic.add(WarReportLogic.WAR_REPORT_4, combatInfo, jjcRank, jjcTargetRank) 
end

function checkDelRole(human)
	local blackList = human.db.friendBlack
	if blackList then
		for uuid, _ in pairs(blackList) do
           local targetDB, online = RoleDBLogic.getDb(uuid, fields)
           if targetDB == nil then
              human.db.friendBlack[uuid] = nil
           end
        end
    end
end

-- 登录
function onLogin(human)

    checkDelRole(human)

    sendFriendList(human)
    sendRequestList(human, true)
end

-- 每日刷新 
function refreshDailyTask(human)
    human.db.sendHeart = nil
    -- 清空赠送记录
    human.db.getHeartCnt = nil
    -- 清空已领取过的状态
    human.db.sendHeartCnt = nil
    -- 清空已赠送过的次数
    FriendDBLogic.delAllFriendHeart(human.db._id)
end

-- 刷新单个好友的信息 todo
function refreshFriendInfo(human, uuid)
    -- 获取好友信息
    local isFriend, friendData = FriendDBLogic.isFriend(human.db._id, uuid)
    if not isFriend or friendData == nil then
        return
    end
    
    -- 自己对好友是否有赠送记录
    local giveStatus = nil
    if human.db.sendHeart ~= nil then
       giveStatus = human.db.sendHeart[uuid]
    end

    -- 自己对好友的领取记录
    local getStatus = nil
    if human.db._id > uuid then
       getStatus = friendData.isGetRedHeart2
    else
       getStatus = friendData.isGetRedHeart1
    end

    local msgRet = Msg.gc.GC_FRIEND_UPDATE
    makeFriendNet(msgRet.friend, uuid, human, getStatus, giveStatus)
	Msg.send(msgRet, human.fd)
end

-- 赠送好友红心值
function sendFriendHeart(human, uuid)
    
 --    local flag = RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_205)
	-- if flag ~= true then
	-- 	return Broadcast.sendErr(human, Lang.FRIEND_OPEN_NEED_LV)
	-- end

    ObjHuman.updateDaily(human)

    local itemList = {}
    local send = nil
    if uuid ~= "" then
	    -- 判定是否是好友
	    if not FriendDBLogic.isFriend(human.db._id, uuid) then
	        -- 不是好友不能赠送
	        return Broadcast.sendErr(human, Lang.FRIEND_GIFT_ERR_FRIEND)
	    end

	    local sendResult = sendFriendHeartdo(human, uuid, nil, nil, itemList)
	    if sendResult == 1 then
	    	return Broadcast.sendErr(human, Lang.FRIEND_HEART_SEND_CNTERR)
	   	end

	    if sendResult == 2 then
	       -- 赠送过了
	       return Broadcast.sendErr(human, Lang.FRIEND_HEART_ERR_HAD)
	    end


	    send = true
	else
		local sendDo = {0}
        local sendResult = 0
		local cnt, list = FriendDBLogic.getFriendUuids(human.db._id)
		for i = 1, cnt do
		   sendResult = sendFriendHeartdo(human, list[i].uuid, sendDo,nil,itemList)
		end

        if sendResult == 2 then
	       Broadcast.sendErr(human, Lang.FRIEND_HEART_ERR_HAD)
	   	end

        if sendResult == 1 then
	       Broadcast.sendErr(human, Lang.FRIEND_HEART_SEND_CNTERR)
	   	end

		local sendNum = sendDo[1]
		if sendNum > 0 then		
			sendFriendList(human)
			send = true
		end
	end
	if send then
		local sendItems = {}
	    local index = 0
	    for id,cnt in pairs(itemList) do
	    	sendItems[index+1] = {}
	    	sendItems[index+1][1] = id
	    	sendItems[index+1][2] = cnt
	    	index = index + 1
	    end
	    BagLogic.sendItemGetList(human,sendItems, "givebyfriend")
	    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
	end
end

function sendFriendHeartdo(human, uuid, sendNum, yjGetDo, itemList)
    -- 封印之章特权
    local dayMax = FriendDefine.FRIEND_SENDHEART_MAX
    local cjPrivilege = ChengjiuLogic.checkPrivilege(human,ChengjiuDefine.PRIVILEGE_TYPE_3)
    if cjPrivilege then
        dayMax = dayMax + cjPrivilege
    end

    -- 每天可赠送
    if human.db.sendHeartCnt and human.db.sendHeartCnt >= dayMax then
    	return 1
	end

    -- 已经赠送过
    if human.db.sendHeart ~= nil then
        if human.db.sendHeart[uuid] ~= nil then
            return 2
        end
    else
        human.db.sendHeart={}
    end

    -- 标识已经赠送过此玩家
    human.db.sendHeart[uuid] = 1

    human.db.sendHeartCnt = (human.db.sendHeartCnt or 0) + 1

    -- 提高友情值
	ObjHuman.updateFriendHeart(human, 10, "givebyfriend")
    BagLogic.addItem(human, ItemDefine.ITEM_JINBI_ID, 10000, "givebyfriend")
    itemList[ItemDefine.ITEM_FRIEND_ID] = (itemList[ItemDefine.ITEM_FRIEND_ID] or 0) + 10
	itemList[ItemDefine.ITEM_JINBI_ID] = (itemList[ItemDefine.ITEM_JINBI_ID] or 0) + 10000
	
    -- 新增红心可领取记录
    FriendDBLogic.setFriendHeart(human.db._id, uuid, 1, true)

    -- 推送成功消息
    if not sendNum then
    	refreshFriendInfo(human, uuid)
    end

    -- 推送RoleSystem 红标签
	RoleSystemLogic.onDotByUuid(uuid, RoleSystemDefine.ROLE_SYS_ID_205)

	if sendNum then
		sendNum[1] = sendNum[1] + 1		
	end

	DailyTaskLogic.recordDailyTaskFinishCnt(human, DailyTaskLogic.DAILY_TASK_ID_2, 1)
end

-- 领取好友红心值
function getFriendHeart(human, uuid)
    ObjHuman.updateDaily(human)

    local itemList = {}
    local getResult = getFriendHeartdo(human,uuid,false,itemList)
    if getResult == 1 then
       Broadcast.sendErr(human, Lang.FRIEND_HEART_GET_NOTFRIEND)
    elseif getResult == 2 then
       Broadcast.sendErr(human, Lang.FRIEND_HEART_GET_HAD)
    elseif getResult == 3 then
       Broadcast.sendErr(human, Lang.FRIEND_HEART_GET_CNTERR)
    end
    
    local sendItems = {}
    local index = 0
    for id,cnt in pairs(itemList) do
    	sendItems[index+1] = {}
    	sendItems[index+1][1] = id
    	sendItems[index+1][2] = cnt
    	index = index + 1
    end
    BagLogic.sendItemGetList(human,sendItems, "givebyfriend")

    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
end


function getFriendHeartdo(human, uuid, yjGetDo, itemList)

    -- 判定是否是好友
    local isFriend, friendData = FriendDBLogic.isFriend(human.db._id, uuid)
    if not isFriend or friendData == nil then
        return 1
    end

    -- 判定每日限制次数
    if human.db.getHeartCnt and human.db.getHeartCnt >= FriendDefine.REDHEART_CNT_MAX then
        return 3
    end

    -- 判定领取状态
    if human.db._id > uuid then
       if friendData.isGetRedHeart2 ~= 1 then
          return 2
       end
    else
       if friendData.isGetRedHeart1 ~= 1 then
          return 2
       end
    end

    if human.db.getHeartCnt == nil then
        human.db.getHeartCnt=0
    end


	
    -- 标识状态已领取状态
    FriendDBLogic.setFriendHeart(human.db._id, uuid, 2, false)
    -- 增加次数
    human.db.getHeartCnt = human.db.getHeartCnt + 1
    -- 提高友情值
    ObjHuman.updateFriendHeart(human, 10, "givebyfriend")
    BagLogic.addItem(human, ItemDefine.ITEM_JINBI_ID, 10000, "givebyfriend")

	itemList[ItemDefine.ITEM_FRIEND_ID] = (itemList[ItemDefine.ITEM_FRIEND_ID] or 0) + 10
	itemList[ItemDefine.ITEM_JINBI_ID] = (itemList[ItemDefine.ITEM_JINBI_ID] or 0) + 10000
	
    -- 推送成功消息
    if not yjGetDo then
    	refreshFriendInfo(human, uuid)
    end
    -- 关闭自己提示
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)

    --
    --Broadcast.sendErr(human, Lang.FRIEND_HEART_GET_SUCESS)
end

-- 一键领取赠送友情值
function getHeartOneTouch(human)

    ObjHuman.updateDaily(human)

    local touchUuidList = {}
	local cnt, list = FriendDBLogic.getFriendUuids(human.db._id)
    for i = 1, cnt do
       touchUuidList[i] = list[i].uuid
    end

	local sendDo = {0}
	local itemList = {}
    local sendResult = 0
    for _, v in ipairs(touchUuidList) do
        -- 领取
        sendResult = getFriendHeartdo(human, v, true, itemList)
	end 

    if sendResult == 2 then
	   --Broadcast.sendErr(human, Lang.FRIEND_HEART_GET_HAD)
	end

    if sendResult == 3 then
	   Broadcast.sendErr(human, Lang.FRIEND_HEART_GET_CNTERR)
	end
	
    local sendItems = {}
    local index = 0
    for id,cnt in pairs(itemList) do
    	sendItems[index+1] = {}
    	sendItems[index+1][1] = id
    	sendItems[index+1][2] = cnt
    	index = index + 1
    end
    BagLogic.sendItemGetList(human,sendItems, "givebyfriend")

	sendFriendList(human)

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_205)
end

-- 好友红心提醒判定
function isDot(human)
	local cnt, list = FriendDBLogic.getFriendUuids(human.db._id)
	for i = 1, cnt do
		local getStatus = list[i].getStatus or 0
		if not human.db.getHeartCnt  or (human.db.getHeartCnt and human.db.getHeartCnt < FriendDefine.REDHEART_CNT_MAX) then
			-- 有可领取的
			if getStatus == 1 then
				return true
			end 
		end
		
		-- if human.db.sendHeart == nil or human.db.sendHeart[list[i].uuid] == nil then
		-- 	-- 有可以赠送的
		-- 	return true
		-- end
	end	

	local questNum = FriendDBLogic.haveRequest(human.db._id)
	if questNum > 0 then
		return true
	end

    return false        
end

function findFriend(human,val)
    local net = nil
	local len = 0
    local msgRet = Msg.gc.GC_FRIEND_FIND
    net,len = RoleDBLogic.getDbByNameRegex(val,fields)
    msgRet.friendList[0] = 0
    for i = 1,len do
    	local uuid = net[i].uuid
    	if isBlackFriendCanDo(human.db._id,uuid) then 
            makeFriendNet(msgRet.friendList[i], uuid, human)
            msgRet.friendList[0] = msgRet.friendList[0] + 1
        end
    end
    Msg.send(msgRet,human.fd)
end

--黑名单
function CG_FRIEND_BLACK_QUERY(human)
	local msgRet = Msg.gc.GC_FRIEND_BLACK_QUERY
	local blackList = human.db.friendBlack
	msgRet.lists[0] = 0
	if blackList then
		for uuid, _ in pairs(blackList) do
			msgRet.lists[0] = msgRet.lists[0] + 1
			makeFriendNet(msgRet.lists[msgRet.lists[0]], uuid, human)
		end
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)
end

--加入黑名单
function CG_FRIEND_BLACK_ADD(human,uuid)
	if human.db.friendBlack and human.db.friendBlack[uuid] then 
        return Broadcast.sendErr(human, Lang.FRIEND_IN_BLACK)
    end
    
	human.db.friendBlack = human.db.friendBlack or {}

	local cnt = 0
	for _,uuid in pairs(human.db.friendBlack) do
		cnt = cnt + 1
	end
	if cnt >= FriendDefine.FRIEND_BLACK_MAX then
		return Broadcast.sendErr(human,Lang.FRIEND_BLACK_MAX)
	end

	human.db.friendBlack[uuid] = true

	-- 删除申请
	delRequest(human.db._id, uuid)
	delRequest(uuid, human.db._id)

	delFriend(human, uuid)

	local msgRet = Msg.gc.GC_FRIEND_BLACK_ADD
	makeFriendNet(msgRet.add, uuid, human)

	Msg.send(msgRet,human.fd)
    Broadcast.sendErr(human, Lang.FRIEND_GOTO_BLACK)
end

--移除黑名单
function CG_FRIEND_BLACK_DEL(human,uuid)
	if uuid ~= "" then
		if not human.db.friendBlack or not human.db.friendBlack[uuid] then return end

		human.db.friendBlack[uuid] = nil
	else
		human.db.friendBlack = nil
	end

	local msgRet = Msg.gc.GC_FRIEND_BLACK_DEL
	msgRet.uuid = uuid
	Msg.send(msgRet,human.fd)
end

--是否存在于黑名单内决定下一步操作
function isBlackFriendCanDo(uuid1,uuid2,sendErr)
	if FriendDBLogic.isBlackFriend(uuid1,uuid2) then
		if sendErr then
			--Broadcast.sendErr(human,Lang.FRIEND_BLACK_ERR1)
		end
		return
	end

	if FriendDBLogic.isBlackFriend(uuid2,uuid1) then
		if sendErr then
			--Broadcast.sendErr(human,Lang.FRIEND_BLACK_ERR2)
		end
		return
	end

	return true
end